home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / src / pvmcruft.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  20KB  |  1,107 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: pvmcruft.c,v 1.15 1997/06/27 19:19:34 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34.  *    cruft.c
  35.  *
  36.  *    Missing links and other wonk.
  37.  *
  38. $Log: pvmcruft.c,v $
  39.  * Revision 1.15  1997/06/27  19:19:34  pvmsrc
  40.  * Integrated WIN32 changes.
  41.  *
  42.  * Revision 1.14  1997/05/27  14:47:04  pvmsrc
  43.  * New message type added: TM_SHMCONN.
  44.  *
  45.  * Revision 1.13  1997/04/30  21:26:10  pvmsrc
  46.  * SGI Compiler Warning Cleanup.
  47.  *
  48.  * Revision 1.12  1997/04/21  14:58:23  pvmsrc
  49.  * Changed #ifdefs that checked IMA_RS6K,IMA_SP2MPI & IMA_AIX46K
  50.  *     to see if select.h was needed into single define NEEDSSELECTH.
  51.  *     New archs need to set this in conf/
  52.  *
  53.  * Revision 1.11  1997/04/08  19:41:41  pvmsrc
  54.  * Added "DM_RESET", "DM_RESETACK" to taglist2...
  55.  *
  56.  * Revision 1.10  1997/04/08  17:23:33  pvmsrc
  57.  * Damn, Damn, Damn...
  58.  *     - pvmreset() must be in library side only, pvmds use different
  59.  *         message buffer mgt, packing and sending routines...  :-Q
  60.  *
  61.  * Revision 1.9  1997/04/08  17:08:17  pvmsrc
  62.  * Oops...  need to pass in caller's tid, lest they be reset, too...
  63.  *
  64.  * Revision 1.8  1997/04/07  20:51:18  pvmsrc
  65.  * Added pvmreset() routine.
  66.  *     - for use by "pvm" console / XPVM, etc.
  67.  *     - wipe out tasks, and tell master pvmd to clean up mboxes.
  68.  *
  69.  * Revision 1.7  1997/03/06  21:50:23  pvmsrc
  70.  * Yanked out #includes for <netinet/in.h> and <netinet/tcp.h>.
  71.  *     - dups with lpvm.h #includes...
  72.  *
  73.  * Revision 1.6  1997/03/06  21:09:47  pvmsrc
  74.  *     use dclock() for pvmgetclock()
  75.  *
  76.  * Revision 1.5  1997/01/28  19:27:00  pvmsrc
  77.  * New Copyright Notice & Authors.
  78.  *
  79.  * Revision 1.4  1996/10/25  13:57:57  pvmsrc
  80.  * Replaced old #includes for protocol headers:
  81.  *     - <pvmsdpro.h>, "ddpro.h", "tdpro.h"
  82.  * With #include of new combined header:
  83.  *     - <pvmproto.h>
  84.  *
  85.  * Revision 1.3  1996/10/24  21:30:39  pvmsrc
  86.  * Moved #include "global.h" to below other #includes for typing.
  87.  * Added #include of new "lpvm.h" & replaced explicit externs.
  88.  * Added new trace control constants:
  89.  *     - TM_TRACER, for pvm_reg_tracer() messages.
  90.  *     - TC_SETTRACE, TC_SETTRCBUF, TC_SETTRCOPT, for on-the-fly setopt
  91.  *         control message / handlers.
  92.  *
  93.  * Revision 1.2  1996/09/27  13:17:09  pvmsrc
  94.  * Added special #include of <sys/select.h> for old RS6K (AIX 3.2):
  95.  *     - fd_set declarations not found in <sys/time.h> on those systems,
  96.  *         or evidently in SP2MPI, need <sys/select.h>.
  97.  *     - similar fixes already existed in hoster.c, lpvm.c and pvmd.c.
  98.  *
  99.  * Revision 1.1  1996/09/23  23:44:29  pvmsrc
  100.  * Initial revision
  101.  *
  102.  * Revision 1.10  1995/11/02  16:21:34  manchek
  103.  * made pvmhdump write to pvm log.
  104.  * new switch NEEDSFFS for ffs.
  105.  * added pvmcopyenv, pvmenvinsert and pvmfreeenv
  106.  *
  107.  * Revision 1.9  1995/07/19  21:26:27  manchek
  108.  * new function pvmnametag converts message tag to name or decimal string
  109.  *
  110.  * Revision 1.8  1995/07/18  17:01:35  manchek
  111.  * added function pvmcrcappend to generate 32-bit crcs
  112.  *
  113.  * Revision 1.7  1995/05/24  16:58:42  manchek
  114.  * whacked doofy coding style in pvmhdump
  115.  *
  116.  * Revision 1.6  1995/05/17  16:27:52  manchek
  117.  * added support for CSPP subcomplexes (/tmp file naming)
  118.  *
  119.  * Revision 1.5  1994/11/07  22:48:09  manchek
  120.  * added tweaks for SX3, SCO
  121.  *
  122.  * Revision 1.4  1994/07/18  19:21:05  manchek
  123.  * pvmputenv() matched variable name substrings (need to compare = too)
  124.  *
  125.  * Revision 1.3  1994/06/03  20:38:22  manchek
  126.  * version 3.3.0
  127.  *
  128.  * Revision 1.2  1993/10/04  20:28:27  manchek
  129.  * added pvmdsockfile() function
  130.  *
  131.  * Revision 1.1  1993/08/30  23:26:50  manchek
  132.  * Initial revision
  133.  *
  134.  */
  135.  
  136. #include <stdio.h>
  137. #ifdef IMA_TITN
  138. #include <bsd/sys/types.h>
  139. #else
  140. #include <sys/types.h>
  141. #endif
  142. #ifndef WIN32
  143. #include <sys/time.h>
  144. #include <sys/socket.h>
  145. #else 
  146. #include <time.h>
  147. #endif
  148. #ifdef NEEDSSELECTH
  149. #include <sys/select.h>
  150. #endif
  151. #include <sys/stat.h>
  152. #include <ctype.h>
  153. #ifdef    SYSVSTR
  154. #include <string.h>
  155. #define    CINDEX(s,c)    strchr(s,c)
  156. #else
  157. #include <strings.h>
  158. #define    CINDEX(s,c)    index(s,c)
  159. #endif
  160. #include <pvm3.h>
  161. #include <pvmproto.h>
  162. #include "pvmalloc.h"
  163. #include "lpvm.h"
  164. #include "global.h"
  165.  
  166.  
  167. /***************
  168.  **  Globals  **
  169.  **           **
  170.  ***************/
  171.  
  172. char *getenv();
  173.  
  174. #ifdef WIN32
  175. extern char *username;
  176. #endif
  177.  
  178. /***************
  179.  **  Private  **
  180.  **           **
  181.  ***************/
  182.  
  183.  
  184. int
  185. pvmput32(p, n)
  186.     char *p;
  187.     int n;
  188. {
  189.     *p++ = n >> 24;
  190.     *p++ = n >> 16;
  191.     *p++ = n >> 8;
  192.     *p = n;
  193.     return 0;
  194. }
  195.  
  196.  
  197. int
  198. pvmput16(p, n)
  199.     char *p;
  200.     int n;
  201. {
  202.     *p++ = n >> 8;
  203.     *p = n;
  204.     return 0;
  205. }
  206.  
  207.  
  208. int
  209. pvmput8(p, n)
  210.     char *p;
  211.     int n;
  212. {
  213.     *p = n;
  214.     return 0;
  215. }
  216.  
  217.  
  218. int
  219. pvmget32(p)
  220.     char *p;
  221. {
  222.     return ((0xff & p[0]) << 24)
  223.     + ((0xff & p[1]) << 16)
  224.     + ((0xff & p[2]) << 8)
  225.     + (0xff & p[3]);
  226. }
  227.  
  228.  
  229. int
  230. pvmget16(p)
  231.     char *p;
  232. {
  233.     return ((0xff & p[0]) << 8) + (0xff & p[1]);
  234. }
  235.  
  236.  
  237. int
  238. pvmget8(p)
  239.     char *p;
  240. {
  241.     return 0xff & *p;
  242. }
  243.  
  244.  
  245. #ifdef NOGETDTBLSIZ
  246.  
  247. #ifdef IMA_TITN
  248.  
  249. int
  250. getdtablesize()
  251. {
  252.     return 64;
  253. }
  254.  
  255. #else    /*IMA_TITN*/
  256.  
  257. #ifdef IMA_SX3
  258.  
  259. int
  260. getdtablesize()
  261. {
  262.     return 64*8;
  263. }
  264.  
  265. #else /*IMA_SX3*/
  266.  
  267. #ifdef WIN32
  268. int
  269. getdtablesize()
  270. {
  271.     return 64;
  272. }
  273. #else
  274. #include <unistd.h>
  275. int
  276. getdtablesize()
  277. {
  278.     return sysconf(_SC_OPEN_MAX);
  279. }
  280. #endif
  281.  
  282. #endif /*IMA_SX3*/
  283.  
  284. #endif    /*IMA_TITN*/
  285.  
  286. #endif /*NOGETDTBLSIZ*/
  287.  
  288. #ifdef    NEEDSFFS
  289. int
  290. ffs(x)
  291.     int x;
  292. {
  293.     int n = 1, m = 1;
  294.  
  295.     if (!x)
  296.         return 0;
  297.     while (!(x & m)) {
  298.         m += m;
  299.         n++;
  300.     }
  301.     return n;
  302. }
  303.  
  304. #endif
  305.  
  306.  
  307. extern char **environ;
  308.  
  309. /*    pvmputenv()
  310. *
  311. *    Replacement for systems that don't have putenv.
  312. *    Tries to be conservative, i.e. doesn't assume that environ
  313. *    points to malloc'd space.
  314. *
  315. *    String must never be freed...
  316. */
  317.  
  318. int
  319. pvmputenv(s)
  320.     char *s;    /* string of form "NAME=Value" */
  321. {
  322.     char *p;                    /* gp */
  323.     int l;                        /* length of env var name or env */
  324.     static char **myen = 0;        /* last environ that i created */
  325.     char **newen;                /* new env if extending */
  326.     char **ep, **ep2;            /* gp */
  327.  
  328.     /* sanity check the string and locate the '=' */
  329.  
  330.     if (!s)
  331.         return -1;
  332.     for (p = s; *p && *p != '='; p++);
  333.     if (p == s || !*p)
  334.         return -1;
  335.     l = p - s + 1;    /* must include '=' */
  336.  
  337.     /* search current environ for name */
  338.  
  339.     for (ep = environ; *ep; ep++)
  340.         if (!strncmp(*ep, s, l))
  341.             break;
  342.  
  343.     if (*ep) {    /* already there, replace it */
  344.         *ep = s;
  345.  
  346.     } else {    /* not there, must extend environ */
  347.  
  348.     /* length needed is? */
  349.  
  350.         for (l = 2, ep = environ; *ep; l++, ep++);
  351.  
  352.         if (environ == myen) {    /* mine, reallocate */
  353.             if (!(newen = (char**)realloc((char *)environ,
  354.                     (unsigned)(l * sizeof(char*)))))
  355.                 return -1;
  356.  
  357.         } else {    /* not mine, alloc new and copy */
  358.             if (!(newen = (char**)malloc((unsigned)(l * sizeof(char*)))))
  359.                 return -1;
  360.             for (ep = environ, ep2 = newen; *ep2++ = *ep++; );
  361.         }
  362.         newen[l - 2] = s;
  363.         newen[l - 1] = 0;
  364.         environ = myen = newen;
  365.     }
  366.     return 0;
  367. }
  368.  
  369.  
  370. /*    pvmcopyenv()
  371. *
  372. *    Make copy of an environment list (null-terminated vector of strings).
  373. *    This is just like environ, but we own all the strings.
  374. *    Can also be used to create an empty list, by passing a null pointer.
  375. *
  376. *    Returns pointer to environment or NULL if malloc fails.
  377. */
  378.  
  379. char **
  380. pvmcopyenv(anyep)
  381.     char **anyep;        /* old environment to copy, or NULL */
  382. {
  383.     char **newep;
  384.     int i, n;
  385.  
  386.     if (anyep) {
  387.         for (n = 0; anyep[n]; n++) ;
  388.         if (newep = TALLOC(n + 1, char *, "env")) {
  389.             newep[n] = 0;
  390.             for (i = 0; i < n; i++) {
  391.                 if (!(newep[i] = STRALLOC(anyep[i]))) {
  392.                     while (i-- > 0)
  393.                         PVM_FREE(newep[i]);
  394.                     PVM_FREE(newep);
  395.                     newep = 0;
  396.                     break;
  397.                 }
  398.             }
  399.         }
  400.  
  401.     } else {
  402.         if (newep = TALLOC(1, char *, "env"))
  403.             newep[0] = 0;
  404.     }
  405.     return newep;
  406. }
  407.  
  408.  
  409. /*    pvmenvinsert()
  410. *
  411. *    Put a copy of a string into an environment list (made by pvmcopyenv).
  412. *    List must be one of ours, since we malloc and free the strings.
  413. *
  414. *    Returns zero or success, or -1 if malloc fails.
  415. *    May update pointer to environment on return.
  416. */
  417.  
  418. int
  419. pvmenvinsert(anep, s)
  420.     char ***anep;        /* environment pointer, may change (NOT environ) */
  421.     char *s;            /* string of form "NAME=Value" */
  422. {
  423.     char **ep;
  424.     char **newep;
  425.     char *p;
  426.     int l;                        /* length of env var name or env */
  427.  
  428.     newep = *anep;
  429.  
  430.     if (!s || !(p = CINDEX(s, '=')))
  431.         return -1;
  432.     l = p - s + 1;            /* must include the '=' */
  433.  
  434.     /* search environ for name */
  435.  
  436.     for (ep = newep; *ep; ep++)
  437.         if (!strncmp(*ep, s, l))
  438.             break;
  439.  
  440.     if (*ep) {                /* already there, replace it */
  441.         PVM_FREE(*ep);
  442.         *ep = STRALLOC(s);
  443.  
  444.     } else {                /* not there, must extend environ */
  445.  
  446.     /* length needed is? */
  447.  
  448.         for (l = 2, ep = newep; *ep; l++, ep++);
  449.  
  450.         if (!(newep = TREALLOC((char *)newep, l, char *)))
  451.             return -1;
  452.  
  453.         newep[l - 2] = STRALLOC(s);
  454.         newep[l - 1] = 0;
  455.         *anep = newep;
  456.     }
  457.     return 0;
  458. }
  459.  
  460.  
  461. /*    pvmfreeenv()
  462. *
  463. *    Free an environment list (made by pvmcopyenv).
  464. */
  465.  
  466. int
  467. pvmfreeenv(anep)
  468.     char **anep;
  469. {
  470.     char **p;
  471.  
  472.     for (p = anep; *p; p++)
  473.         PVM_FREE(*p);
  474.     PVM_FREE(anep);
  475.     return 0;
  476. }
  477.  
  478.  
  479. /*    pvmxtoi()
  480. *
  481. *    Yet another version of ascii hex to integer
  482. */
  483.  
  484. pvmxtoi(p)
  485.     char *p;
  486. {
  487.     int i = 0;
  488.     char c;
  489.  
  490.     if (p[0] == '0' && (p[1] == 'x' || p[1] == 'X'))
  491.         p += 2;
  492.     while (isxdigit(c = *p++)) {
  493.         i = (i << 4) + c - (isdigit(c) ? '0' : (isupper(c) ? 'A' : 'a') - 10);
  494.     }
  495.     return i;
  496. }
  497.  
  498.  
  499. /*    pvmstrtoi()
  500. *
  501. *    Convert string in decimal, hex or octal to integer.
  502. */
  503.  
  504. int
  505. pvmstrtoi(p)
  506.     char *p;
  507. {
  508.     int i = 0;
  509.     char c;
  510.  
  511.     if (*p == '0') {
  512.         p++;
  513.         if (*p == 'x' || *p == 'X')
  514.             while (isxdigit(c = *++p))
  515.                 i = (i << 4) + c
  516.                         - (isdigit(c) ? '0' : (isupper(c) ? 'A' : 'a') - 10);
  517.         else
  518.             while (c = *p++, c >= '0' && c <= '7')
  519.                 i = (i << 3) + c - '0';
  520.         return i;
  521.     } else
  522.         return (int)atoi(p);
  523. }
  524.  
  525.  
  526. #ifndef    I860_NODE
  527.  
  528. /*    hex_inadport()
  529. *
  530. *    Takes a string of format 00000000:0000 and returns a sockaddr_in.
  531. */
  532.  
  533. void
  534. hex_inadport(s, sad)
  535.     char *s;
  536.     struct sockaddr_in *sad;
  537. {
  538.     sad->sin_addr.s_addr = htonl((unsigned)pvmxtoi(s));
  539.     s = (s = CINDEX(s, ':')) ? s + 1 : "";
  540.     sad->sin_port = htons((unsigned short)pvmxtoi(s));
  541. }
  542.  
  543.  
  544. /*    inadport_decimal()
  545. *
  546. *    Returns printable string corr. to sockaddr_in with addr in decimal.
  547. */
  548.  
  549. char *
  550. inadport_decimal(sad)
  551.     struct sockaddr_in *sad;
  552. {
  553.     static char buf[32];
  554.     int a;
  555.  
  556.     a = ntohl(0xffffffff & sad->sin_addr.s_addr);
  557.     sprintf(buf, "%d.%d.%d.%d:%d",
  558.             0xff & (a >> 24),
  559.             0xff & (a >> 16),
  560.             0xff & (a >> 8),
  561.             0xff & a,
  562.             0xffff & (int)ntohs(sad->sin_port));
  563.     return buf;
  564. }
  565.  
  566.  
  567. /*    inadport_hex()
  568. *
  569. *    Returns printable string corr. to sockaddr_in with addr in hex.
  570. */
  571.  
  572. char *
  573. inadport_hex(sad)
  574.     struct sockaddr_in *sad;
  575. {
  576.     static char buf[16];
  577.     int a;
  578.  
  579.     a = ntohl(0xffffffff & sad->sin_addr.s_addr);
  580.     sprintf(buf, "%08x:%04x", a, 0xffff & (int)ntohs(sad->sin_port));
  581.     return buf;
  582. }
  583.  
  584. #endif    /*I860_NODE*/
  585.  
  586.  
  587. #ifdef    NOTMPNAM
  588. char *
  589. pvmtmpnam(buf)
  590.     char *buf;
  591. {
  592.     static int n = 0;
  593.     static char scratch[32];
  594.  
  595. #ifndef WIN32
  596.  
  597.     if (!buf)
  598.         buf = scratch;
  599.     sprintf(buf, "/tmp/tmp%06d.%d", getpid(), n++ % 10000);
  600.  
  601. #else
  602.  
  603.     char tmptmp[32];
  604.  
  605.     if (!buf)
  606.         buf = scratch;
  607.     if (!getenv("PVM_TMP")) {
  608.         fprintf(stderr,
  609.                 "Could not get PVM_TMP, defaulting to %Temp% \n");
  610.         if (!getenv("TEMP")) {
  611.             fprintf(stderr,
  612.                     "Could not get TEMP, defaulting to c:\temp \n");
  613.              sprintf(buf,"c:\temp");
  614.         } else  sprintf(buf,getenv("TEMP"));
  615.     } else sprintf(buf,getenv("PVM_TMP"));
  616.  
  617.     sprintf(tmptmp, "/tmp%06d.%d", _getpid(), n++ % 10000);
  618.     strcat(buf,tmptmp);
  619.  
  620. #endif
  621.  
  622.     return buf;
  623. }
  624. #endif    /*NOTMPNAM*/
  625.  
  626.  
  627. pvmhdump(cp, n, pad)
  628.     char *cp;        /* bytes */
  629.     int n;            /* length */
  630.     char *pad;        /* tag string */
  631. {
  632.     static char *buf = 0;
  633.     static int bufl = 0;
  634.  
  635.     int i, l;
  636.     char *r;
  637.  
  638.     pad = pad ? pad : "";
  639.     l = strlen(pad) + 50;
  640.     if (l > bufl) {
  641.         if (buf)
  642.             PVM_FREE(buf);
  643.         if (!(buf = TALLOC(l, char, "hdump"))) {
  644.             bufl = 0;
  645.             pvmlogerror("pvmhdump() malloc failed\n");
  646.             return 1;
  647.         }
  648.         bufl = l;
  649.     }
  650.  
  651.     r = buf;
  652.     for (i = 0; n-- > 0; i = (i + 1) & 15) {
  653.         sprintf(r, "%s%02x",
  654.             i ? " " : pad,
  655.             0xff & *cp++);
  656.         r += strlen(r);
  657.         if (!n || i == 15) {
  658.             strcpy(r, "\n");
  659.             pvmlogerror(buf);
  660.             r = buf;
  661.         }
  662.     }
  663.  
  664.     return 0;
  665. }
  666.  
  667.  
  668. /*    pvmgethome()
  669. *
  670. *    Return path of user homedir.
  671. */
  672.  
  673. char *
  674. pvmgethome()
  675. {
  676.     static char *hd = 0;
  677.  
  678.     if (!hd) {
  679.         if (hd = getenv("HOME")) {
  680.             hd = STRALLOC(hd);
  681.  
  682.         } else {
  683.             pvmlogerror("warning: can't getenv(HOME), using /\n");
  684.             hd = "/";
  685.         }
  686.     }
  687.     return hd;
  688. }
  689.  
  690.  
  691. /*    pvmgetroot()
  692. *
  693. *    Return absolute path of PVM top level directory.
  694. */
  695.  
  696. char *
  697. pvmgetroot()
  698. {
  699.     static char *rd = 0;
  700.  
  701.     if (!rd && !(rd = getenv("PVM_ROOT"))) {
  702.         pvmlogerror("PVM_ROOT environment variable not set.\n");
  703. #ifdef IMA_CSPP
  704.         {
  705.             struct stat buf;
  706.  
  707.             rd = STRALLOC("/usr/convex/pvm");
  708.             if (stat(rd, &buf) == -1) {
  709.                 pvmlogperror("Unable to default PVM_ROOT to /usr/convex/pvm");
  710.                 pvmbailout(0);
  711.                 exit(1);        /* the other meaning of bail out */
  712.             }
  713.             pvmputenv("PVM_ROOT=/usr/convex/pvm");
  714.             pvmlogerror("Defaulting PVM_ROOT to /usr/convex/pvm");
  715.         }
  716. #endif
  717.         pvmbailout(0);
  718.         exit(1);        /* the other meaning of bail out */
  719.     }
  720.     return rd;
  721. }
  722.  
  723.  
  724. /*    pvmgetpvmd()
  725. *
  726. *    Return absolute path of pvmd startup script.
  727. */
  728.  
  729. char *
  730. pvmgetpvmd()
  731. {
  732.     char *r;
  733.     static char *pp = 0;
  734.  
  735.     if (!pp) {
  736.         if (PVMDFILE[0] == '/')
  737.             pp = PVMDFILE;
  738.         else {
  739.             r = pvmgetroot();
  740.             pp = TALLOC(strlen(r) + strlen(PVMDFILE) + 2, char, "pvmdpath");
  741.             sprintf(pp, "%s/%s", r, PVMDFILE);
  742.         }
  743.     }
  744.     return pp;
  745. }
  746.  
  747.  
  748. /*    pvmdsockfile()
  749. *
  750. *    Returns name of file where pvmd advertises its sockaddr.
  751. *    Assumes global pvm_useruid is set before calling.
  752. */
  753.  
  754. char *
  755. pvmdsockfile()
  756. {
  757.     static char buf[160];
  758. #ifdef WIN32
  759.     char TDSOCKSPEC[256];
  760. #endif
  761.     char hna[128];
  762.     char *p;
  763. #ifdef    IMA_CSPP
  764.     int scid = get_scid();    /* default (system) subcomplex ID is 1 */
  765. #endif
  766.  
  767. #ifdef    SHAREDTMP
  768.  
  769.     if (gethostname(hna, sizeof(hna)-1) == -1) {
  770.         pvmlogerror("pvmdsockfile() can't gethostname()\n");
  771.         return (char*)0;
  772.     }
  773.     if (p = CINDEX(hna, '.'))
  774.         *p = 0;
  775.  
  776. #ifdef    IMA_CSPP
  777.     if (scid > 1)
  778.         (void)sprintf(buf, TDSOCKNAME_CSPP, pvm_useruid, scid, hna);
  779.     else
  780. #endif
  781.         (void)sprintf(buf, TDSOCKNAME, pvm_useruid, hna);
  782.  
  783. #else
  784.  
  785. #ifdef    IMA_CSPP
  786.     if (scid > 1)
  787.         (void)sprintf(buf, TDSOCKNAME_CSPP, pvm_useruid, scid);
  788.     else
  789. #endif
  790. #ifdef WIN32
  791.         if (!getenv("PVM_TMP"))
  792.         {
  793.             (void)sprintf(buf, TDSOCKNAME, username);
  794.             pvmlogprintf("Could not get PVM_TMP, continuing with %s\n",
  795.                     buf);
  796.         } else {
  797.             strcpy(TDSOCKSPEC,getenv("PVM_TMP"));
  798.             if (_chdir(getenv("PVM_TMP")) == -1) {
  799.                 pvmlogerror("Could Not retrieve PVM_TMP directory \n");
  800.                 pvmbailout(0);
  801.             }
  802.             sprintf(buf,"/pvmd.%s",username);
  803.             strcat(TDSOCKSPEC,buf);
  804.             sprintf(buf,TDSOCKSPEC);
  805.         }
  806. #else
  807.         (void)sprintf(buf, TDSOCKNAME, pvm_useruid);
  808. #endif
  809.  
  810. #endif
  811.  
  812.     return buf;
  813. }
  814.  
  815.  
  816. #ifdef IMA_CSPP
  817. #include <sys/cnx_pattr.h>
  818.  
  819. /*    get_scid ()
  820. *
  821. *    (Convex CSPP only)
  822. *    Returns the subcomplex identifier for the calling process
  823. */
  824.  
  825. int
  826. get_scid ()
  827. {
  828.     static int my_scid = -1;
  829.  
  830.     int rc;
  831.     struct cnx_pattributes pattrib;
  832.  
  833.     if (my_scid < 0) {
  834.         my_scid = 0;
  835.         if ((rc = cnx_getpattr(getpid(), CNX_PATTR_SCID, &pattrib)) != 0) {
  836.             pvmlogperror ("get_scid: Unable to get scid");
  837.             return 0;
  838.         }
  839.         my_scid = pattrib.pattr_scid;
  840.     }
  841.     return my_scid;
  842. }
  843. #endif    /*IMA_CSPP*/
  844.  
  845.  
  846. #define    PVMCRCPOLY    0xedb88320
  847.  
  848. unsigned int
  849. pvmcrcappend(buf, len, crc)
  850.     char *buf;            /* buffer to append to crc value */
  851.     int len;            /* length */
  852.     unsigned int crc;    /* initial value */
  853. {
  854.     static unsigned int crctbl[256];
  855.     static int once = 1;
  856.  
  857.     if (once) {
  858.         int i, j;
  859.         unsigned int v;
  860.  
  861.         for (i = 0; i < 256; i++) {
  862.             v = i;
  863.             for (j = 8; j--; )
  864.                 v = v & 1 ? (v >> 1) ^ PVMCRCPOLY : v >> 1;
  865.             crctbl[i] = v;
  866.         }
  867.         once = 0;
  868.     }
  869.  
  870.     while (len-- > 0)
  871.         crc = ((crc >> 8) & 0x00ffffff) ^ crctbl[(crc ^ *buf++) & 0xff];
  872.     return crc;
  873. }
  874.  
  875.  
  876. static char *taglist1[] = {
  877.     "TM_CONNECT",
  878.     "TM_CONN2",
  879.     "TM_EXIT",
  880.     "TM_ADDHOST",
  881.     "TM_DELHOST",
  882.     "TM_CONFIG",
  883.     "TM_MSTAT",
  884.     "TM_HALT",
  885.     "TM_TICKLE",
  886.     "TM_SPAWN",
  887.     "TM_PSTAT",
  888.     "TM_SENDSIG",
  889.     "TM_TASK",
  890.     "TM_MCA",
  891.     "TM_NOTIFY",
  892.     "TM_DB",
  893.     "TM_SCHED",
  894.     "TM_TASKER",
  895.     "TM_HOSTER",
  896.     "TM_TRACER",
  897.     "TM_HOSTSYNC",
  898.     "TM_SETOPT",
  899.     "TM_GETOPT",
  900.     "TM_CONTEXT",
  901.     "TM_SHMCONN",
  902. };
  903.  
  904. static char *taglist2[] = {
  905.     "DM_ADD", "DM_ADDACK",
  906.     "DM_EXEC", "DM_EXECACK",
  907.     "DM_SENDSIG",
  908.     "DM_HTUPD", "DM_HTUPDACK", "DM_HTCOMMIT",
  909.     "DM_SLCONF",
  910.     "DM_STARTACK",
  911.     "DM_TASK", "DM_TASKACK",
  912.     "DM_DELHOST", "DM_DELHOSTACK",
  913.     "DM_NULL",
  914.     "DM_TASKOUT",
  915.     "DM_PSTAT", "DM_PSTATACK",
  916.     "DM_HALT",
  917.     "DM_MCA",
  918.     "DM_NOTIFY", "DM_NOTIFYACK",
  919.     "DM_DB", "DM_DBACK",
  920.     "DM_RESET", "DM_RESETACK",
  921.     "DM_HTDEL",
  922.     "DM_HOSTSYNC", "DM_HOSTSYNCACK",
  923. };
  924.  
  925. static char *taglist3[] = {
  926.     "TC_CONREQ",
  927.     "TC_CONACK",
  928.     "TC_TASKEXIT",
  929.     "TC_NOOP",
  930.     "TC_OUTPUT",
  931.     "TC_SETTRACE",
  932.     "TC_SETTMASK",
  933.     "TC_SETTRCBUF",
  934.     "TC_SETTRCOPT",
  935.     "TC_SHMAT",
  936.     "TC_OUTPUTX",
  937.     "TC_SIBLINGS",
  938. };
  939.  
  940. static char *taglist4[] = {
  941.     "SM_SPAWN",
  942.     "SM_EXEC", "SM_EXECACK",
  943.     "SM_TASK",
  944.     "SM_CONFIG",
  945.     "SM_ADDHOST",
  946.     "SM_DELHOST",
  947.     "SM_ADD", "SM_ADDACK",
  948.     "SM_NOTIFY",
  949.     "SM_TASKX",
  950.     "SM_HOSTX",
  951.     "SM_HANDOFF",
  952.     "SM_SCHED",
  953.     "SM_STHOST", "SM_STHOSTACK",
  954.     "SM_STTASK",
  955. };
  956.  
  957. struct nametag {
  958.     int first;
  959.     int last;
  960.     char **names;
  961. };
  962.  
  963. static struct nametag nametaglist[] = {
  964.     { TM_CONNECT, TM_SHMCONN, taglist1 },
  965.     { DM_ADD, DM_HOSTSYNCACK, taglist2 },
  966.     { TC_CONREQ, TC_SIBLINGS, taglist3 },
  967.     { SM_SPAWN, SM_STTASK, taglist4 },
  968. };
  969.  
  970.  
  971. /*    pvmnametag()
  972. *
  973. *    Convert message tag to symbolic name for reserved tags.
  974. *    Returns name string or decimal tag if not found.
  975. *    If found is nonnull, return whether found.
  976. */
  977.  
  978. char *
  979. pvmnametag(tag, found)
  980.     int tag;
  981.     int *found;
  982. {
  983.     static char buf[32];
  984.  
  985.     int i, j;
  986.  
  987.     for (i = sizeof(nametaglist)/sizeof(nametaglist[0]); i-- > 0; )
  988.         if (tag >= nametaglist[i].first && tag <= nametaglist[i].last) {
  989.             j = tag - nametaglist[i].first;
  990.             if (found)
  991.                 *found = 1;
  992.             return nametaglist[i].names[j];
  993.         }
  994.     sprintf(buf, "%d", tag);
  995.     if (found)
  996.         *found = 0;
  997.     return buf;
  998. }
  999.  
  1000.  
  1001. /*    debug_flags()
  1002. *
  1003. *    Convert debug mask to flag names
  1004. */
  1005.  
  1006. static char *dflgs[] = {
  1007.     "pkt",    /* 1 */
  1008.     "msg",    /* 2 */
  1009.     "tsk",    /* 4 */
  1010.     "slv",    /* 8 */
  1011.     "hst",    /* 10 */
  1012.     "sel",    /* 20 */
  1013.     "net",    /* 40 */
  1014.     "mpp",    /* 80 */
  1015.     "sch",    /* 100 */
  1016.     "app",    /* 200 */
  1017.     "wai",    /* 400 */
  1018.     "mem",    /* 800 */
  1019.     "sem",    /* 1000 */
  1020.     "lck",    /* 2000 */
  1021.     "rtg",    /* 4000 */
  1022.     "mbx"    /* 8000 */
  1023. };
  1024.  
  1025. char *
  1026. debug_flags(mask)
  1027.     int mask;
  1028. {
  1029.     static char buf[64];
  1030.     int bit, i;
  1031.  
  1032.     buf[0] = 0;
  1033.     for (bit = 1, i = 0; i < sizeof(dflgs)/sizeof(dflgs[0]); i++, bit *= 2)
  1034.         if (mask & bit) {
  1035.             if (buf[0])
  1036.                 strcat(buf, ",");
  1037.             strcat(buf, dflgs[i]);
  1038.         }
  1039.     return buf;
  1040. }
  1041.  
  1042.  
  1043. /*    print_fdset()
  1044. *
  1045. *    Print an fd_set in human-readable format.
  1046. */
  1047.  
  1048. print_fdset(pad, n, f)
  1049.     char *pad;        /* label at head */
  1050.     int n;            /* max fd + 1 */
  1051.     fd_set *f;        /* fd set */
  1052. {
  1053.     int i;
  1054.     char *s = "";
  1055.  
  1056.     pvmlogprintf("%s", pad);
  1057.     for (i = 0; i < n; i++)
  1058.         if (FD_ISSET(i, f)) {
  1059.             pvmlogprintf("%s%d", s, i);
  1060.             s = ",";
  1061.         }
  1062.     pvmlogprintf("\n");
  1063.     return 0;
  1064. }
  1065.  
  1066.  
  1067. #ifdef I860_NODE
  1068. int
  1069. gettimeofday(tp, tzp)
  1070.     struct timeval *tp;
  1071.     struct timezone *tzp;
  1072. {
  1073.     double t, dclock();
  1074.  
  1075.     t = dclock();
  1076.     tp->tv_sec = t;
  1077.     tp->tv_usec = (t - tp->tv_sec)*1.0E06;
  1078.     return 0;
  1079. }
  1080. #endif
  1081.  
  1082.  
  1083. /*    pvmgetclock()
  1084. *
  1085. *    Use this whenever we need a clock value in seconds and microseconds.
  1086. *    Return values shouldn't be compared to those from gettimeofday,
  1087. *    because it doesn't necessarily return the actual time of day.
  1088. */
  1089.  
  1090. int
  1091. pvmgetclock(tm)
  1092.     struct timeval *tm;
  1093. {
  1094. #if defined(IMA_PGON)
  1095.     double dclock();
  1096.     double t;
  1097.  
  1098.     t = dclock();
  1099.     tm->tv_sec = t;
  1100.     tm->tv_usec = (t - tm->tv_sec)*1.0E06;
  1101. #else
  1102.     return gettimeofday(tm, (struct timezone*)0);
  1103. #endif
  1104. }
  1105.  
  1106.  
  1107.